var layer = layui.layer, //弹层
    table = layui.table, //表格
    form = layui.form,
    $ = layui.jquery,
    laydate = layui.laydate;

lay('.starttime').each(function(){
    laydate.render({
        elem: this
    });
});

layui.use(['dropdown'], function () {
    let dropdown = layui.dropdown;
    dropdown.onFilter("statistics", function (event) {
        switch (event) {
            case "statisticalProduct":
                parent.intab("views/jsp/WarehouseManagement/tpl/otherInStatisticalProduct.jsp","其它入库产品统计")
                break
        }
    });
})

let operationList=[];
layer.ready(function () {

    //获取当前用户的部门及人员结构树
    let treeList = getCurrentDepartmentUser();

    if(treeList && treeList.list){
        let userTree = getAllJson(treeList.list);
        operationList=getxmselect("#operationCode","operationNo", userTree);
    }else {
        operationList=getxmselect("#operationCode","operationNo", [{name: user.name, value: user.code}]);
    }


});

form.render();//表单刷新
table.render({
    elem: '#otherwarehouseselect'
    ,where:{operationNo : user.code}
    ,url: 'otherInStock/getListByPage'
    ,toolbar: '#otherwarehousetoolbar'
    ,cols: [[
        {checkbox: true, fixed: true}
        ,{field:'statusStr', title:'单据类型'}
        ,{field:'billNo', title:'入库单号',sort:true,event: "receiptno"}
        ,{field:'createDate', title:'创建日期',sort:true}
        ,{field:'operator', title:'操作人',sort:true}
        ,{field:'person', title:'入库人',sort:true}
        ,{field:'warehouseName', title:'仓库名称',sort:true}
        ,{field:'memo', title:'备注'}
        ,{fixed: 'right', width:240, toolbar: '#otherwarehouseeditor', title:'操作'}
    ]]
    ,id: 'otherwarehousetableid'
    ,height: $(document).height() - $('#otherwarehouseselect').offset().top - 50
    ,page: true
});
table.on('tool(otherwarehousetable)', function(obj){
    let data = obj.data;
    let id = data.id;
    if(obj.event === 'update') {
        parent.intab("views/jsp/WarehouseManagement/tpl/updateOtherInStock.jsp?id="+obj.data.id,"修改其它入库")
    }else if(obj.event === 'delect'){
        layer.confirm('确认删除？',confirmInfo(3,6), function (index) {
            layer.close(index);
            let dataBases={
                id:id,
                status:data.status
            }
            $.ajax({
                type:"POST",
                dataType:"json",
                contentType: 'application/json',
                data:JSON.stringify(dataBases),
                url:"otherInStock/delete",
                success:function(res){
                    if(res.status == 200){
                        layer.msg("删除成功", {icon: 6});
                        table.reload('otherwarehousetableid', {
                            page: {
                                limit:10,
                                curr: 1 //重新从第 1 页开始
                            }
                            ,where: data.field
                        }, 'data');
                        return false;
                    }else {
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            });
        });
    }else if(obj.event === 'submits'){
        layer.confirm('确定提交审批吗', confirmInfo(3, 5), function (index) {
            operationData(data, 1, "提交审批");
        });
    }else if(obj.event === 'through'){
        layer.confirm('确定审批通过吗', confirmInfo(3, 5), function (index) {
            operationData(data, 2, "审批通过");
        });
    }else if(obj.event === 'backtothe'){
        layer.confirm('确定审批退回吗', confirmInfo(3, 5), function (index) {
            operationData(data, 3, "审批退回");
        });
    }else if(obj.event === 'complete'){
        layer.confirm('确定完成吗', confirmInfo(3, 5), function (index) {
            operationData(data, 4, "完成");
        });
    }else if(obj.event === 'reduction'){
        layer.confirm('确定还原吗', confirmInfo(3, 5), function (index) {
            operationData(data, 5, "还原");
        });
    }else if (obj.event === "receiptno"){
        parent.intab("views/jsp/WarehouseManagement/tpl/viewotherwarehouse.jsp?id="+obj.data.id,"其它入库明细")
    }
});

function operationData(data, operation, msg){
    let fields = {
        id : data.id,
        billNo : data.billNo,
        operation: operation
    }
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:JSON.stringify(fields),
        url:"otherInStock/operation",
        success:function(res){
            if(res.status == 200){
                layer.msg(msg, {icon: 6});
                table.reload('otherwarehousetableid', {
                    page: {
                        limit:10,
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: data.field
                }, 'data');
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
}

table.on('toolbar(otherwarehousetable)', function(obj){
    switch(obj.event){
        case 'news':
            parent.intab("views/jsp/WarehouseManagement/tpl/newupotherwarehouse.jsp","新增其它入库")
            break;
    };
});

form.on('submit(submitform)', function(data){
    if (data.field.operationNo==null || data.field.operationNo===""){
        data.field.operationNo=user.code;
    }
    table.reload('otherwarehousetableid', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        ,where: data.field
    }, 'data');
    form.render();//表单刷新
    return false;
});

$("#statistical").on("click",function () {
    parent.intab("views/jsp/WarehouseManagement/tpl/statistics.jsp","其他入库统计")
})

//提示信息渲染
function confirmInfo(icon, anim) {
    let data = {
        icon: icon,
        title: "提示",
        offset: "auto",
        skin: 'layui-layer-molv',
        anim: anim
    }
    return data;
}
document.getElementById('clear').onclick = function(){
    operationList.setValue([ ]);
};
function getAllJson(jsons) {
    let tmpitem=[]
    for (let item of jsons) {
        let tmparr={}
        tmparr["value"]=item["code"];
        tmparr["name"]=item["name"];
        if(item["users"]){
            for(let it of item["users"]){
                item["list"].push(it)
            }
        }
        if(item["list"] && item["list"].length){
            tmparr["children"]=   getAllJson(item["list"]);
            tmpitem.push(tmparr)
        }else{
            tmpitem.push(tmparr)
        }

    }
    return tmpitem
}
function getxmselect(el,name, data) {
    return xmSelect.render({
        el: el,
        name: name,
        tree: {
            show: true,
            showLine: true,
            showFolderIcon:true,
            indent: 20,
            expandedKeys:false
        },
        height: 'auto',
        data: data,
        on: function ({ arr, change, isAdd }) {
            return this
        }
    })
}
